/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.zeos.bean;

import br.zeos.ZDataSource;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import javax.swing.JTextField;
import javax.swing.text.Document;
import br.zeos.util.MaskMoney;
import java.util.regex.Pattern;

/**
 *
 * @author hardware
 */
public class ZInputMoney extends JTextField implements ActionListener {

    private ZDataSource origem = null;
    private String FieldName = "";

    public ZDataSource getDataSource() {
        return origem;
    }

    public void setDataSource(final ZDataSource origem) {
        this.origem = origem;
        this.origem.addUpdateListener(this);
        this.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent ke) {
                updateData();
            }

            @Override
            public void keyPressed(KeyEvent ke) {
                updateData();
            }

            @Override
            public void keyReleased(KeyEvent ke) {
                updateData();
            }
        });
    }

    private void updateData() {
        //arrumar
        HashMap<String, String> linha = this.origem.getLine();
        linha.put(FieldName.toUpperCase(), this.getText());
        this.origem.setLinha(linha);
    }

    public String getFieldName() {
        return FieldName;
    }

    public void setFieldName(String FieldName) {
        this.FieldName = FieldName;
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        String valor = origem.getLine().get(this.getFieldName().toUpperCase());
        this.setText(valor);
        //    this.setText(this.getText().replace("00", ""));
    }

    @Override
    public void setText(String t) {
        if (t.contains(".") || t.contains(",")) {
            int menor = (t.length() - ((t.lastIndexOf(".") >= t.lastIndexOf(",")) ? t.lastIndexOf(".") : t.lastIndexOf(","))) - 1;
            t = t.replaceAll(Pattern.quote("."), "");
            t = t.replaceAll(Pattern.quote(","), "");
            t = t.substring(0, t.length() - menor) + "." + t.substring(t.length() - menor);
            if (menor == 1) {
                t += "0";
            }
        } else {
            t += ".00";
        }
        t = t.replace(".", "");
        try {
            t = Integer.parseInt(t) + "";
        } catch (Exception e) {
//            e.printStackTrace();
        }

        super.setText(t); //To change body of generated methods, choose Tools | Templates.
    }

    public ZInputMoney() {
        setPOG();
    }

    public ZInputMoney(String text) {
        super(text);
//        setPOG();
    }

    public ZInputMoney(int columns) {
        super(columns);
//        setPOG();
    }

    public ZInputMoney(String text, int columns) {
        super(text, columns);
//        setPOG();
    }

    public ZInputMoney(Document doc, String text, int columns) {
        super(doc, text, columns);
//        setPOG();
    }

    private void setPOG() {
        this.setDocument(new MaskMoney());
    }
}
